﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "SwitchTo3DViewCmd.h"
#include "GbmpUiPlatformCommandIds.h"

#include "IApplication.h"
#include "IUiDocumentViewManager.h"
#include "IMainWindow.h"

#include "IUiDocument.h"
#include "IUiView.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "IDocument.h"
#include "IModelView.h"
#include "IUserTransaction.h"
#include "GcmpCommandState.h"
#include "IPlane.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"


using namespace gcmp;

#define View3DViewType L"View3DViewType"
#define BMVI_3D L"BMVI_3D"

namespace
{
    std::vector<IModelView*> GetModelViewByType(IDocument* pDoc, const std::wstring& viewType)
    {
        std::vector<IModelView*> views;
        DBG_WARN_AND_RETURN_UNLESS(pDoc != nullptr, views, L"pDoc不能为空",L"GDMPLab",L"2024-03-30");

        std::vector<IModelView*> allViews = IModelView::GetAllModelViews(pDoc);
        FOR_EACH(pView, allViews)
        {
            if (pView->GetUserDataId() == viewType)

            {
                views.push_back(pView);
            }
        }
        return views;
    }

    IModelView* Create3DOrthView(IDocument* pDoc, const std::wstring& viewName,
        const Vector3d& viewDirection, const Vector3d& origin, const Vector3d& rightDirection, const Vector3d upDirection)
    {
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

        std::wstring viewType = pDoc->IsFamilyDocument() ? BMVI_3D : View3DViewType;
        IModelView* pView = IModelView::Create(pDoc, viewName, BuiltInViewType::ThreeDimensional, viewDirection, origin, rightDirection, upDirection);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pView, L"创建ModelView失败",L"GDMPLab",L"2024-03-30");
        pView->SetUserDataId(viewType);


        // 构造默认工作平面：三维视图的默认工作平面取XOY面
        OwnerPtr<IPlane> plane = IPlane::Create(pView->GetOrigin(), Vector3d(1, 0, 0), Vector3d(0, 1, 0));
        DBG_WARN_AND_RETURN_FALSE_UNLESS(plane, L"plane为空",L"GDMPLab",L"2024-03-30");
        bool bOk = pView->SetWorkPlane(plane.get());
        DBG_WARN_AND_RETURN_FALSE_UNLESS(bOk, L"SetWorkPlane失败",L"GDMPLab",L"2024-03-30");

        return pView;
    }
}

SwitchTo3DViewCmd::SwitchTo3DViewCmd()
: CommandBase(ID_CMD_SWITCHTO_3DVIEW, true)
{
}

SwitchTo3DViewCmd::~SwitchTo3DViewCmd()
{
}

gcmp::OwnerPtr<IAction> SwitchTo3DViewCmd::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocViewMgr, L"pUiDocViewMgr为空",L"GDMPLab",L"2024-03-30");
    IUiView* pUIView = pUiDocViewMgr->GetCurrentUiView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUIView != nullptr, L"当前没有活动的视图",L"GDMPLab",L"2024-03-30");
    IUiDocument* pUIDoc = pUIView->GetUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUIDoc != nullptr, L"pUIDoc不能为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDocument = pUIDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDocument, L"Active Document为空",L"GDMPLab",L"2024-03-30");

    std::vector<IModelView*> views3D = GetModelViewByType(pDocument, View3DViewType);
    IModelView* pView = nullptr;

    if (views3D.empty())//如果没有三维视图，则创建一个
    {
        OwnerPtr<IUserTransaction> ut = IUserTransaction::Create(pDocument, GBMP_TR(L"初始化三维视图"));
        pView = Create3DOrthView(pDocument, GBMP_TR(L"三维"), Vector3d::Create(0.191258, -0.614716, -0.765209), Vector3d::Create(0.0, 0.0, 0.0),
            Vector3d::Create(0.990155595861602, 0.13997105409318, 0.0), Vector3d::Create(-0.0498104741195676, 0.352359421749768, 0.93453825741494));
       ut->Commit();
    }
    else//打开第一个三维视图
    {
        pView = views3D.at(0);
    }
    if (pView)
    {
        IMainWindow* pMainWindow = IMainWindow::GetMainWindow();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pMainWindow, L"pMainWindow为空",L"GDMPLab",L"2024-03-30");
        pMainWindow->CreateOrOpenUiView(pUIDoc->GetId(), pView->GetElementId(), false, L"");
    }

    return nullptr;
}

bool SwitchTo3DViewCmd::IsVisible() const
{
    return GcmpCommandState::IsInDocumentEnvironment();
}

bool SwitchTo3DViewCmd::IsEnabled() const
{
    IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pUiDocViewMgr, L"pUiDocViewMgr为空",L"GDMPLab",L"2024-03-30");
    IUiView* pCurrentView = pUiDocViewMgr->GetCurrentUiView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pCurrentView != nullptr, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");
    IModelView* pModelView = pCurrentView->GetModelView();
    if (pModelView && pModelView->GetUserDataId() == View3DViewType)

    {
        return false;
    }

    return true;
}
REGISTER_GM_COMMAND(SwitchTo3DViewCmd)


